/*
   @Copyright:LeetCode
   @Author:   tjyemail
   @Problem:  http://leetcode.com/problems/count-and-say
   @Language: C++
   @Datetime: 19-06-05 17:23
   */

class Solution {
public:
	string countAndSay(int n) {
		string res="1";
		char str[32];
		for(int i=1; i<n; ++i){
			string tmp;
			int cnt=1;
			for(int j=1; j<res.length(); ++j){
				if(res[j]!=res[j-1]){
					sprintf(str,"%d%c",cnt,res[j-1]);
					tmp.append(str);
					cnt=1;
				}
				else ++cnt;
			}
			sprintf(str,"%d%c",cnt,res.back());
			tmp.append(str);
			res=tmp;
		}
		return res;
	}
};
